#include <pjmedia/vid_codec_util.h>
#include <pjmedia/errno.h>
#include <pjmedia/stream_common.h>
#include <pjlib-util/base64.h>
#include <pj/ctype.h>
#include <pj/math.h>

#include "od_sip_types.h"
#include "od_trace.h"

#define THIS_FILE   "od_h264_sdp_match_GB28181.c"

#define VALUE_RANGE_PARSE(min, max, my_value, info)  \
    do { \
       if (my_value > max \
             || my_value < min) { \
        RPTERR(info, my_value); \
        return PJ_EINVAL; \
        } \
    }while(0)


PJ_DEF(pj_status_t) od_codec_h264_match_sdp_GB28181(pj_pool_t *pool,
                                                    pjmedia_sdp_media *offer,
                                                    unsigned o_fmt_idx,
                                                    pjmedia_sdp_media *answer,
                                                    unsigned a_fmt_idx,
                                                    unsigned option)
{
    char result[12][16];
    const char *default_f = "v/2/4/25/1/1024a/1/8/1";
    char *p = &result[0][0];
    char *ptr = NULL;
    int i = 0;

    memset(result, 0, sizeof(result));
    
    if (offer->f.slen) {
        ptr = offer->f.ptr+2; //start parse at v/xx
    } else {
        /*if there no f offer, then use default value*/
        ptr = (char *)(default_f + 2);
        pj_str_t src = pj_str((char *)default_f);
        pj_strdup(pool, &offer->f, &src);
    }

    /*save all suitable value to result, then parse illegl*/
    *p = '0';
    while(*ptr && i < 12)
    {
        if (*ptr == 'v'
               || *ptr == '/'
               || *ptr == 'a'){
            ptr++;
            p = &result[++i][0];
            *p = '0';
        } else {
            *p++ = *ptr++;
        }
    }
    
    VALUE_RANGE_PARSE(OD_GB28181_CODEC_VID_MPEG4, OD_GB28181_CODEC_VID_3GP, atoi(&result[0][0]), 
                                                    "video type out of range[1-4], now :%d");
    VALUE_RANGE_PARSE(OD_GB28181_RESO_QCIF, OD_GB28181_RESO_1080P_I, atoi(&result[1][0]), 
                                                    "video reso out of range[1-6], now: %d");
    VALUE_RANGE_PARSE(0, 99, atoi(&result[2][0]), "video reso out of range[1-6], now: %d");
    VALUE_RANGE_PARSE(OD_GB28181_BITRATE_CBR, OD_GB28181_BITRATE_VBR, atoi(&result[3][0]), 
                                                    "video bitrate-type out of range[1-2], now: %d");
    VALUE_RANGE_PARSE(0, 100000, atoi(&result[4][0]), "video bitrate out of range[0-100000], now: %d");
    VALUE_RANGE_PARSE(OD_GB28181_CODEC_AUD_G711, OD_GB28181_CODEC_AUD_G722_1, atoi(&result[6][0]), 
                                                    "audio type out of range[1-4], now: %d");
    VALUE_RANGE_PARSE(OD_GB28181_AUD_BR_5_3K, OD_GB28181_AUD_BR_64K, atoi(&result[7][0]), 
                                                    "audio bitrate out of range[1-8], now: %d");
    VALUE_RANGE_PARSE(OD_GB28181_AUD_SAMPLERATE_8K, OD_GB28181_AUD_SAMPLERATE_32K, atoi(&result[8][0]), 
                                                    "audio samplerate out of range[1-4], now: %d");

    /*don't care origin f memory, it's can be free by pool*/
    pj_strdup(pool, &answer->f, &offer->f);


    RPTDBG("MATCH!!");
    return PJ_SUCCESS;
}


/*_*/

